import passport from 'koa-passport';
import { Strategy as GitHubStrategy } from 'passport-github2';
import dotenv from 'dotenv';
import User from '../models/User';

dotenv.config();

passport.use(new GitHubStrategy({
  clientID: process.env.GITHUB_CLIENT_ID as string,
  clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
  callbackURL: 'http://localhost:3000/auth/github/callback'
}, async (accessToken: any, refreshToken: any, profile: any, done: any) => {
  try {
    const [user, created] = await User.findOrCreate({
      where: { username: profile.username },
      defaults: { password: '' } // No password for OAuth users
    });
    return done(null, user);
  } catch (err) {
    return done(err);
  }
}));